package CTitle
{
	import CExtends.cardElement;
	
	import Classes.pi_events;
	
	import UIFree.ObjectEvent;
	
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.text.TextFormat;

	public class ct_title extends cardElement {
		protected var _component	: Object;
		protected var _back			: Object;
		protected var _backBitmap	: Object;
		protected var _label		: Object;
		private var _backID		: int;
		private var _fontIndex		: int;
		private var _fontLoaders	: Object;
				
		public function ct_title(a_cardBody :Object) {
			super(a_cardBody);
			_fontIndex	= -1;
			_backID	 	= -1;
			_fontLoaders= new Object();
			initComponent();
		}
		
		public function initComponent(): void {
			_component = new app.classes['component'](parentCardBody);
			_component.visible = false;
			_component.addEvent(pi_events.PAINTTOBITMAP, doPaint, parentCardBody);
			
			_back = new app.classes['loader'](_component);
			_back.contentLoaderInfo.addEventListener(Event.COMPLETE, doBackComplete);
			_backBitmap = new app.classes['bitmap']();
			
			_component.addChild(_backBitmap);
			_label = new app.classes['textField']();
		}
		
		public function applyParam(a_backID: int, a_fontIndex: int, titleText: String): void {
			if (a_backID != _backID) applyBack(a_backID);
			if (a_fontIndex != _fontIndex) applyFont(a_fontIndex);
			if (titleText != text) {
				_label.text = titleText;
				parentCardBody.dispatchEvent(new ObjectEvent(pi_events.UPDATECARD, this));
			}
		}
		
		public function applyBack(a_backID: int): void {
			_backID = a_backID;
			_back.load(new URLRequest(ct_data.backsURL + '/games/data/title_backs/' + _backID + '.png'), new LoaderContext(true));
		}
		
		public function applyFont(a_fontIndex: int): void {
			if (a_fontIndex > -1) {
				_fontIndex = a_fontIndex;
				var fName: String = ct_data.fonts[a_fontIndex].name;
				if (!_fontLoaders.hasOwnProperty(fName)) {
					_fontLoaders[fName] = new app.classes.fontContent(_component, ct_data.backsURL + '/games/data/fonts', fName, function(e: Event): void {
						updateFont();
					});
				} else updateFont();
			}
		}
		
		protected function updateFont(): void {
			if ((_fontIndex > -1) && (_fontLoaders[fontInfo.name].fontObject)) {
				_fontLoaders[fontInfo.name].fontObject.applyFont(_label, 16, backInfo.color);
				var tf: TextFormat = new TextFormat();
				var a_tf: Object = fontInfo.tf;
				tf.align = 'center';
				tf.size = 16;
				for (var prop: String in a_tf)
					tf[prop] = a_tf[prop];
				
				tf.color = backInfo.color;
				_label.setTextFormat(tf);
				_label.defaultTextFormat = tf;
				parentCardBody.dispatchEvent(new ObjectEvent(pi_events.UPDATECARD, this));
			}
		}
		
		public function get backURL(): String {
			return _back.sourceUrl;
		}
		
		public function get fontIndex(): int {
			return _fontIndex;
		}
		
		public function get backID(): int {
			return _backID;
		}
		
		protected function get backInfo(): Object {
			return ct_data.fromID(_backID);
		}
		
		protected function get fontInfo(): Object {
			return ct_data.fonts[_fontIndex]
		}
		
		public function get text(): String {
			return _label.text;
		}
		
		private function doBackComplete(le: Event): void {
			parentCardBody.dispatchEvent(new ObjectEvent(pi_events.UPDATECARD, this));
			updateFont();
		}
		
		protected function doPaint(e: ObjectEvent): void {
			if (_back.loaded) {
				var cmatix: Matrix = e.object.matrix.clone();
				if (_backBitmap.bitmapData) _backBitmap.bitmapData.dispose();
				var size: Point = cmatix.transformPoint(new Point(_back.contentLoaderInfo.width, _back.contentLoaderInfo.height));
				var bi: Object = backInfo;
				var mat		: Matrix;
				var scale	: Number;
				
				_backBitmap.bitmapData = new app.classes.bitmapData(_back.contentLoaderInfo.width, _back.contentLoaderInfo.height, true, 0xFFFFFF);
				_backBitmap.bitmapData.draw(_back);
				
				if (_label.text > '') {
					var mt: Object = _label.getLineMetrics(0);
					_label.width  = _label.textWidth + mt.x + 5;
					_label.height = _label.textHeight + mt.descent + mt.leading + 2;
					scale = Math.min(bi.rect[2] / _label.width, bi.rect[3] / _label.height);
					mat = new Matrix();
					mat.scale(scale, scale);
					if (bi.rotation) {
						mat.translate(-bi.rect[2] / 2, -bi.rect[3] / 2);
						mat.rotate(bi.rotation / 180 * Math.PI);
						mat.translate(bi.rect[2] / 2, bi.rect[3] / 2);
					}
					mat.translate(bi.rect[0] + (bi.rect[2] - _label.width * scale) / 2, bi.rect[1] + (bi.rect[3] - _label.height * scale) / 2);
					
					_backBitmap.bitmapData.draw(_label, mat);
				}

				_backBitmap.smoothing = true;
				var width	: Number;
				mat = cmatix;
				var _x: Number, _y	: Number, t_scale: Number;
//				if (e.object.bitmap.bitmapData.width < size.x) 
					t_scale = e.object.bitmap.bitmapData.width / size.x * 0.7;
//				else t_scale = 1;	
				
				if (backInfo.align == 'c') _x = (e.object.bitmap.bitmapData.width - size.x * t_scale) / 2; // По центру горизонтали
				else if (backInfo.align == 'l') _x = 0; // По левому краю
				else if (backInfo.align == 'r') _x = e.object.bitmap.bitmapData.width - size.x * t_scale; // По правому краю
				if (backInfo.valign == 'b') _y = e.object.bitmap.bitmapData.height - size.y * t_scale; // По низу
				else _y = 0; // По верху
				
				mat.scale(t_scale, t_scale);
				mat.translate(_x, _y);
				e.object.bitmap.bitmapData.draw(_backBitmap, mat, null, null, null, true);
			}
		}
		
		override public function dispose(): void {
			_component.dispose();
			parentCardBody.dispatchEvent(new ObjectEvent(pi_events.UPDATECARD, this));
		}
		
		public function get params(): Array {
			var result: Array = ['tlt', backID, fontIndex, c_title.encode(text)];
			if ((backID > -1) && (fontIndex > -1))
				result = result.concat([fontInfo.name, fontInfo.tf.leading, 
										backInfo.rect[0], backInfo.rect[1], backInfo.rect[2], backInfo.rect[3], backInfo.color,
										backInfo.align, backInfo.valign]);
			return result;	
		}
	}
}